﻿using Microsoft.Extensions.DependencyInjection;
using Model.Core.Entity;
using Model.Core.Entity.Dbcontexts;
using Model.Core.Services;
using RobotPratolSolution.Models;
using System.Collections.ObjectModel;
using System.Windows.Media;
using Microsoft.EntityFrameworkCore;

using CommunityToolkit.Mvvm.ComponentModel;
namespace RobotPratolSolution.ViewModels
{
    internal class CronManageViewModel : ObservableObject
    {

        DbcontextBase _DbcontextBase { get; set; }
        public ObservableCollection<JobInfoModel> JobInfoModels { get; set; }
        public ObservableCollection<JobTimeInfo> CronDataList { get; set; }
        LocalRobotInfo LocalRobotInfo { get; set; }
        private string _TakedJobTime;

        public string TakedJobTime
        {
            get { return _TakedJobTime; }
            set { _TakedJobTime = value; }
        }


        public CronManageViewModel()
        {
            CronDataList = new ObservableCollection<JobTimeInfo>();
            JobInfoModels = new ObservableCollection<JobInfoModel>();
            _DbcontextBase = App.ServiceProvider.GetService<DbcontextBase>();
            LocalRobotInfo = App.ServiceProvider.GetService<LocalRobotInfo>();

        }

        public void UpdateCronUseable(string index)
        {
            var showdata = CronDataList.FirstOrDefault(a => a.Id.ToString() == index);
            showdata.IsEnable = !showdata.IsEnable;
            var data = _DbcontextBase.JobCroninfo.FirstOrDefault(a => a.Id.ToString() == index);
            if (data != null)
            {
                data.IsEnable = showdata.IsEnable;
                _DbcontextBase.JobCroninfo.Update(data);
                _DbcontextBase.SaveChanges();
            }
        }
        public void CronListRefresh()
        {
            //string robotid = App.RobotId;
            var datalist = _DbcontextBase.JobCroninfo.Include(a=>a.JobId).AsNoTracking().Where(a => a.RobotId == LocalRobotInfo.RobotId).OrderBy(a => a.Time).ToList();
            if (CronDataList == null)
                CronDataList = new ObservableCollection<JobTimeInfo>();
            else
                CronDataList.Clear();
            for (var i = 0; i < datalist.Count; i++)
            {
                var item = datalist[i];
                CronDataList.Add(new JobTimeInfo()
                {
                    Id = item.Id,
                    JobName = item.JobName,
                    Time = item.Time,
                    JobId = item.JobId.Id.ToString(),
                    RobotId = item.RobotId,
                    IsEnable = item.IsEnable,

                });
            }
        }

        public void AddTimeJobItem(int jobListindex)
        {
            Guid guid = Guid.NewGuid();
            string timestr = this.TakedJobTime;
            string JobName = JobInfoModels[jobListindex].Name;
            string JobId = JobInfoModels[jobListindex].Id;
            string RobotId = LocalRobotInfo.RobotId;
            CronDataList.Add(new JobTimeInfo() { RobotId = RobotId, Id = guid, JobName = JobName, JobId = JobId, Time = timestr, IsEnable = true });
            var jobinfo = _DbcontextBase.JobInfo.FirstOrDefault(a => a.Id.ToString() == JobId);
            _DbcontextBase.JobCroninfo.Add(new JobCronInfo()
            {
                Id = guid,
                JobName = JobName,
                Time = timestr,
                JobId = jobinfo,
                RobotId = RobotId,
                IsEnable = true,                
            });
            _DbcontextBase.SaveChanges();

        }
        public void RemoveTimeJobItem(string jobTimeitem)
        {
            // var job = CronDataList[jobTimeindex];
            var data = _DbcontextBase.JobCroninfo.FirstOrDefault(a => a.Id.ToString() == jobTimeitem);
            _DbcontextBase.JobCroninfo.Remove(data);
            _DbcontextBase.SaveChanges();
            int index = CronDataList.IndexOf(CronDataList.FirstOrDefault(a => a.Id.ToString() == jobTimeitem));
            CronDataList.RemoveAt(index);
        }
        public void UpdateTimeJobItem()
        {

        }

        /// <summary>
        /// 任务列表刷新
        /// </summary>
        public void JobListInfoRefresh()
        {
            var datalist = _DbcontextBase.JobInfo.Where(a => a.RobotId == LocalRobotInfo.RobotId).ToList();
            if (JobInfoModels == null) JobInfoModels = new ObservableCollection<JobInfoModel>();
            else
                JobInfoModels.Clear();
            foreach (var item in datalist)
            {
                JobInfoModels.Add(new JobInfoModel()
                {
                    Name = item.Name,
                    Id = item.Id.ToString(),
                    JobType = item.JobType,
                });
            }
        }

    }


    public class JobTimeInfo : ObservableObject
    {
        private Guid _Id;

        public Guid Id
        {
            get { return _Id; }
            set { _Id = value; }
        }



        private string _Time;

        public string Time
        {
            get { return _Time; }
            set { _Time = value; }
        }

        private string _JobName;

        public string JobName
        {
            get { return _JobName; }
            set { _JobName = value; }
        }

        private string _JobId;

        public string JobId
        {
            get { return _JobId; }
            set { _JobId = value; }
        }

        private string _RobotId;
        public string RobotId
        {
            get { return _RobotId; }
            set { _RobotId = value; }
        }

        private string _RobotName;
        public string RobotName
        {
            get { return _RobotName; }
            set { _RobotName = value; }
        }

        private bool _IsEnable;
        public bool IsEnable
        {
            get { return _IsEnable; }
            set
            {
                _IsEnable=value;
                OnPropertyChanged(nameof(EnableStr));
                OnPropertyChanged(nameof(EnableClor));
            }
        }
        public string EnableStr
        {
            get { return IsEnable ? "启用" : "未启用"; }
        }
        public SolidColorBrush EnableClor
        {
            get { return IsEnable ? new SolidColorBrush(Colors.Green) : new SolidColorBrush(Colors.Red); }
        }

        //IAsyncCommand EnableCommand { get;set; }

        //public JobTimeInfo()
        //{
        //    //EnableCommand = new 
        // //   EnableCommand.CanExecuteChanged += EnableCommand_CanExecuteChanged;
        //}

        //private void EnableCommand_CanExecuteChanged(object? sender, EventArgs e)
        //{
        //    IsEnable =!IsEnable;
        //}
    }
}
